// 服务路由器
// 1.引入模块
const express = require('express')
const pool = require('../pool.js')
// 2.创建路由
const router = express.Router()
// 3.暴露路由
module.exports = router

// //先写一个简单的接口测试一下
//测试地址:http://127.0.0.1:3000/v1/sxguangyin/show
// router.get('/show',(req,res)=>{
// 	res.send('hello')
// })
//导航接口
router.get('/nav',(req,res)=>{
	let sql = 'select * from nav'
	pool.query(sql,(err,result)=>{
		if(err) throw err
		// console.log(result)
		for(var i=0;i<result.length;i++){
			result[i].children=[]
		}
		let sql='select n2.* from nav n1 join nav_second n2 where n1.id=n2.parent_id'
		pool.query(sql,(err,result2)=>{
			if(err) throw err
			// console.log(result2)
			
			for(var  i=0;i<result.length;i++){
				for(var j=0;j<result2.length;j++){
				    if(result2[j].parent_id==result[i].id){
				        result[i].children.push(result2[j]);
				    }
				}
			}
			// console.log(result)
			
			res.send({code:200,date:result})
		})
	})
})
//轮播图1接口
router.get('/banner1',(req,res)=>{
    let sql='select img_src from banner1'
	pool.query(sql,(err,result)=>{
        if(err) throw err;
        res.send({'code':200,'result':result});
    })
})

//轮播图2接口
router.get('/banner2',(req,res)=>{
    let sql='select img_src from banner2'
	pool.query(sql,(err,result)=>{
        if(err) throw err;
        res.send({'code':200,'result':result});
    })
})

//关于我们接口
router.get('/aboutus',(req,res)=>{
	let id=req.query.parent_id
	let sql='select title,content from AboutUs where parent_id=?'
	pool.query(sql,[id],(err,result)=>{
		if(err) throw err
		res.send({'code':200,'result':result});
	})
})

//新闻中心接口
router.get('/newsDetails',(req,res)=>{
	let id=req.query.id
	let sql='select * from News where id=?'
	pool.query(sql,[id],(err,result)=>{
		if(err) throw err
		res.send({'code':200,'result':result});
	})
})

router.get('/news',(req,res)=>{
	// let id=req.body.id
	let sql='select * from News '
	pool.query(sql,(err,result)=>{
		if(err) throw err
		res.send({'code':200,'result':result});
	})
})